package com.lazy.app.ui.act;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.lazy.app.R;
import com.lazy.app.databinding.ActivityAboutBinding;
import com.lazy.app.presenter.AboutPresenter;
import com.lazy.app.presenter.H5Presenter;
import com.zndroid.base.ui.common.CommonActivity;
import com.zndroid.utils.XUtils;
import com.zndroid.utils.impl.SpannableStringUtil;
import com.zndroid.widget.title.TitleBar;

public class AboutActivity extends CommonActivity<ActivityAboutBinding, AboutPresenter> {
    @Override
    protected int onSupplyStatusBarColor() {
        return ResourcesCompat.getColor(getResources(), R.color.color_white, getTheme());
    }

    @Override
    protected TitleBar onSupplyTitleBar() {
        return currentViewBinding().tbTitle;
    }

    @Override
    protected ActivityAboutBinding onSupplyViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityAboutBinding.inflate(layoutInflater);
    }

    @Override
    protected AboutPresenter onSupplyPresenter() {
        return new AboutPresenter();
    }

    @Override
    protected void doOnCreate(@Nullable Bundle savedInstanceState) {
        super.doOnCreate(savedInstanceState);

        XUtils.create().screenUtil().setStatusBarDarkMode(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void doOnInitViewAfterAllReady(View rootView) {
        updateAppInfoAndTerms();
        updateFunctionItems();
    }

    private void updateAppInfoAndTerms() {
        //app info
        currentViewBinding().ivLogo.setImageResource(getApplicationInfo().icon);
        currentViewBinding().tvAppName.setText(XUtils.create().appUtil().getApplicationName(this));
        currentViewBinding().tvAppVersion.setText("Version " + XUtils.create().appUtil().getVersionName(this));

        //terms
        SpannableStringUtil.with(currentViewBinding().tvAppInfo)
                .append("Lazy软件许可及服务协议").setFontSize(14, true)
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Intent intent = new Intent(AboutActivity.this, H5Activity.class);

                        intent.putExtra(H5Presenter.H5_KEY_TITLE, "用户协议");
                        intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_USER_AGREEMENT);
                        intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_USER_AGREEMENT);
                        intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

                        toActivityCallBackAble(intent);
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.argb(255, 87, 107, 149));
                        ds.setUnderlineText(true);
                    }
                })
                .appendLine("")
                .append("《隐私政策》").setFontSize(14, true)
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Intent intent = new Intent(AboutActivity.this, H5Activity.class);

                        intent.putExtra(H5Presenter.H5_KEY_TITLE, "隐私保护政策");
                        intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_PRIVACY_POLICY);
                        intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_PRIVACY_POLICY);
                        intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

                        toActivityCallBackAble(intent);
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.argb(255, 87, 107, 149));
                        ds.setUnderlineText(true);
                    }
                })
                .appendLine("")
                .append("Copyright ©2021-2022 lazy.").setFontSize(12, true).setForegroundColor(ResourcesCompat.getColor(getResources(), R.color.color_grey_dark, getTheme()))
                .appendLine("")
                .append("All rights reserved.").setFontSize(12, true).setForegroundColor(ResourcesCompat.getColor(getResources(), R.color.color_grey_dark, getTheme()))
                .create();
    }

    private void updateFunctionItems() {
        //检查更新
        currentViewBinding().tvCheckUpgrade.setOnClickListener(v -> {
            showToast(getString(R.string.txt_temp));
        });
        //官网
        currentViewBinding().tvWebSite.setOnClickListener(v -> {
            Intent intent = new Intent(currentActivity(), H5Activity.class);

            intent.putExtra(H5Presenter.H5_KEY_TITLE, "官网");
            intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

            toActivityCallBackAble(intent);
        });
        //功能介绍
        currentViewBinding().tvShowFunction.setOnClickListener(v -> {
            Intent intent = new Intent(currentActivity(), H5Activity.class);

            intent.putExtra(H5Presenter.H5_KEY_TITLE, "官网");
            intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

            toActivityCallBackAble(intent);
        });
        //帮助中心
        currentViewBinding().tvHelpCenter.setOnClickListener(v -> {
            Intent intent = new Intent(currentActivity(), H5Activity.class);

            intent.putExtra(H5Presenter.H5_KEY_TITLE, "官网");
            intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

            toActivityCallBackAble(intent);
        });
        //调试中心
        currentViewBinding().tvDebugCenter.setOnClickListener(v -> {
            toActivityCallBackAble(new Intent(currentActivity(), DeveloperGospelActivity.class));
        });
        //用户反馈
        currentViewBinding().tvFeedback.setOnClickListener(v -> {
            Intent intent = new Intent(currentActivity(), H5Activity.class);

            intent.putExtra(H5Presenter.H5_KEY_TITLE, "官网");
            intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_OFFICE_WEB_SIT);
            intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

            toActivityCallBackAble(intent);
        });
    }
}
